/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class SimpleOption extends AbstractOption {

	protected Agent agent;
	
	protected Map<State, Double> initiationSet;
	
	protected Set<State> terminationSet;
	
	public SimpleOption(Agent a) {
		this("SimpleOption", a);
	}
	
	public SimpleOption(String name, Agent a) {
		super(name);
		setAgent(a);
		initiationSet = new HashMap<State, Double>();
		terminationSet = new HashSet<State>();
	}
	
	@Override
	public <T> double getBeta(State<T> s) {
		if ( terminationSet.contains(s) )
			return 1;
		Double beta = initiationSet.get(s);
		if ( beta == null )
			return 1;
		return beta;
	}

	@Override
	public <T> void update(State<T> s, Option o, double update) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public <T> Action doStep(State<T> s, double reward) {
		Action a = agent.doStep(s, reward);
		if ( Math.random() < getBeta(s) ) {
			setFinished();
			return null;
		}
		return a;
	}

	@Override
	public <T> Action firstStep(State<T> s) {
		Action a = agent.firstStep(s);
		if ( Math.random() < getBeta(s) ) {
			setFinished();
			return null;
		}
		return a;
	}

	@Override
	public <T> void lastStep(State<T> s, double reward) {
		agent.lastStep(s, reward);
		setFinished();
	}
	
	@Override
	public double getLearnRate() {
		return agent.getLearnRate();
	}

	@Override
	public Policy getPolicy() {
		return agent.getPolicy();
	}

	@Override
	public void setLearnRate(double alpha) {
		agent.setLearnRate(alpha);		
	}

	@Override
	public void setPolicy(Policy p) {
		agent.setPolicy(p);		
	}
	
	@Override
	public <T> boolean isEligible(State<T> s) {
		return initiationSet.containsKey(s);
	}
	
	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public void setInitiationSet(Map<State, Double> initiationSet) {
		this.initiationSet = initiationSet;
	}

	public void setTerminationSet(Set terminationSet) {
		this.terminationSet = terminationSet;
	}
	
	public void addInitiationState(State s, double beta) {
		initiationSet.put(s, beta);
	}
	
	public void addTerminationState(State terminationState) {
		terminationSet.add(terminationState);
	}

}
